package com.msi.manning.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class Result extends Activity {
    private static final String CLASSTAG = Result.class.getSimpleName();

    private TextView factof;
    private TextView calories;
    private TextView total_fat;
    private TextView sodium;
    private TextView cholestrl;
    private String choice;
    
    public void onCreate(final Bundle icicle, String xml) {
        super.onCreate(icicle);

        factof = (TextView) findViewById(R.id.factof);
        calories = (TextView) findViewById(R.id.calories_val);
        total_fat = (TextView) findViewById(R.id.total_fat_val);
        sodium = (TextView) findViewById(R.id.sodium_val);
        cholestrl = (TextView) findViewById(R.id.cholestrl_val);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null)
        	choice = extras.getString("choice");
        else
        	startActivity(new Intent(null, NutritionMe.class));
        
        Log.d(Constants.LOGTAG, " " + Result.CLASSTAG + " Choice = " + choice);
        
        final String url = "http://teamsupercool.x10hosting.com/result.php?keyword=" + choice;

        HashMap<String,String> facts = getHttpResponse(url);
        
        factof.setText(choice);
        calories.setText(facts.get("Energ_Kcal"));
        total_fat.setText(facts.get("Tot_Fat"));
        sodium.setText(facts.get("Sodium"));
        cholestrl.setText(facts.get("Cholestrl"));
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        // inflate the SAME view XML layout file as ApacheHTTPWithAuth Activity (re-use it)
        this.setContentView(R.layout.result);
    }
    
    /**
     * Perform an HTTP GET with HttpUrlConnection.
     * 
     * @param location
     * @return
     */
    private HashMap<String,String> getHttpResponse(String location) {
        URL url = null;
        HashMap<String, String> items = new HashMap<String, String>();
        
        Log.d(Constants.LOGTAG, " " + Result.CLASSTAG + " location = " + location);

        try {
            url = new URL(location);
            Log.d(Constants.LOGTAG, " " + Result.CLASSTAG + " url = " + url);
        } catch (MalformedURLException e) {
            Log.e(Constants.LOGTAG, " " + Result.CLASSTAG + " " + e.getMessage());
        }

        if (url != null) {

           // this.progressDialog = ProgressDialog.show(this, "working . . .", "performing HTTP request");
        
        	
            try {
                HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
                BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
                String inputLine;

                //choices.setAdapter(new ListAdapter());

                while ((inputLine = in.readLine()) != null) {
                	if(!inputLine.equals("")){
                		Log.v(Constants.LOGTAG, " " + Result.CLASSTAG + " inputLine = " + inputLine);
                		String[] input = inputLine.split(":");
                		items.put(input[0], input[1]);
                	}
                }

                in.close();
                urlConn.disconnect();

            } catch (IOException e) {
                Log.e(Constants.LOGTAG, " " + Result.CLASSTAG + " " + e.getMessage());
            }

            //this.progressDialog.dismiss();
        } else {
            Log.e(Constants.LOGTAG, " " + Result.CLASSTAG + " url NULL");
        }
        return items;
    }
    
    
    private final Handler handler = new Handler() {

    };
}
